package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.service.IAgencyCertificationAuditService;
import com.jzo2o.customer.service.IAgencyCertificationService;
import com.jzo2o.customer.service.IServeProviderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AgencyCertificationAuditServiceImpl extends ServiceImpl<AgencyCertificationAuditMapper, AgencyCertificationAudit> implements IAgencyCertificationAuditService {
    @Resource
    private IAgencyCertificationService agencyCertificationService;
    @Resource
    private IServeProviderService serveProviderService;
    @Override
    public void audit(AgencyCertificationAuditAddReqDTO agencyCertificationAuditAddReqDTO) {
        //新增申请资质认证记录
        AgencyCertificationAudit agencyCertificationAudit = BeanUtil.toBean(agencyCertificationAuditAddReqDTO, AgencyCertificationAudit.class);
        agencyCertificationAudit.setAuditStatus(0);
        baseMapper.insert(agencyCertificationAudit);
        //查询认证记录
        Long serveProviderId = agencyCertificationAuditAddReqDTO.getServeProviderId();
        AgencyCertification agencyCertification = agencyCertificationService.getById(serveProviderId);
        if(ObjectUtil.isNotNull(agencyCertification)){
            //将认证信息状态更新为认证中
            agencyCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            agencyCertificationService.updateById(agencyCertification);
        }else{
            agencyCertification = new AgencyCertification();
            agencyCertification.setId(serveProviderId);
            agencyCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            agencyCertificationService.save(agencyCertification);
        }
    }

    @Override
    public RejectReasonResDTO queryCurrentUserLastRejectReason() {
        LambdaQueryWrapper<AgencyCertificationAudit> queryWrapper = Wrappers.<AgencyCertificationAudit>lambdaQuery().eq(AgencyCertificationAudit::getServeProviderId, UserContext.currentUserId()).orderByDesc(AgencyCertificationAudit::getCreateTime).last("limit 1");
        AgencyCertificationAudit agencyCertificationAudit = baseMapper.selectOne(queryWrapper);
        return new RejectReasonResDTO(agencyCertificationAudit.getRejectReason());
    }
}
